package com.ruoyi.web.controller.api.user;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.R;

import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.Test.HttpsService;
import com.ruoyi.common.utils.Test.MD5Util;
import com.ruoyi.common.utils.Test.PrivacyBindBodyAxb;
import com.ruoyi.common.utils.Test.PrivacyUnbindBody;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.binary.Base64;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @author zcc
 * @date 2022/8/29
 * @apiNote
 */
@RestController
@RequestMapping(value = "/virtual/")
public class VirtualController {


    @ApiOperation(value = "绑定虚拟电话")
    @RequestMapping(value = "/VirtualPhone", method = RequestMethod.POST)
    public R<String> deptList(@Validated @RequestBody Map<String, Object> map) throws Exception {
        Integer miao = null;
        PrivacyBindBodyAxb bindBodyAxb = new PrivacyBindBodyAxb();
        String phone = "";//用户手机号
        String phone1 = "";//专家手机号
        String str = "13041430152";
        /** AXB模式小号绑定接口地址*/
        String url = "https://101.37.133.245:11008/voice/1.0.0/middleNumberAXB";
        /** 设定绑定的隐私小号*/
        String trumpet = str;
        bindBodyAxb.setMiddleNumber(trumpet);
        /** 设定与该隐私小号绑定的号码A*/
        bindBodyAxb.setBindNumberA(phone1);//专家
        /** 设定与该隐私小号绑定的号码B*/
        bindBodyAxb.setBindNumberB(phone);//用户
        /** 设置是否开启通话录音  1:开启，0:关闭*/
        bindBodyAxb.setCallRec(1);
        /** 设置绑定关系有效时长 ,为空表示绑定关系永久，单位:秒*/
        //计算剩余时间秒 按照当前时间
        Date date = new Date();
        SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String format = s.format(new Date());
        //根据订单号查询预约时间
        Date gettime = date;
        if(null==gettime){
            bindBodyAxb.setMaxBindingTime(1800); //30分钟
        }else {
            String format1 = s.format(gettime);
            long b = s.parse(format).getTime();
            long a = s.parse(format1).getTime();
            int c = (int) ((a - b) / 1000);
            bindBodyAxb.setMaxBindingTime(c);
        }
        /** 设置是否透传主叫的号码到A  0:不透传; 1: 透传，不填默认不透传*/
        bindBodyAxb.setPassthroughCallerToA(1);
        /** 设置是否透传主叫的号码到B  0:不透传; 1: 透传，不填默认不透传*/
        bindBodyAxb.setPassthroughCallerToB(1);
        //提示结束时间
        bindBodyAxb.setMaxCallDuration(1800);
//        /** 设置用于接收呼叫结果的服务地址*/
        bindBodyAxb.setCallbackUrl("https://zhjt.cnshangji.cn/api/front/user/expert/resultsOf");
        /** 获取当前系统时间戳*/
        long timestamp = System.currentTimeMillis();
        /** 生成Base64转码后的参数authorization*/
        String name = 494608+":"+timestamp;
        byte[] bytes = name.getBytes();
        String authorization = Base64.encodeBase64String(bytes);
        /** 生成加密参数sig*/
        String sig = MD5Util.getMD5(494608 + "7d22b0d568ba4fe1ac4e842ec98de9cd" + timestamp);
        /** 生成最终接口访问地址*/
        url = url + "/" + 494608 + "/" + sig;
        String body = JSON.toJSONString(bindBodyAxb);
        /** 调用接口*/
        HttpsService httpsService = new HttpsService();
        String result = httpsService.doPost(url, authorization, body, "UTF-8");
        System.out.println("------------------"+result);
        return R.ok(str);
    }

    @ApiOperation(value = "解绑虚拟电话")
    @RequestMapping(value = "/unbundling", method = RequestMethod.POST)
    public  R<String> httpsPrivacyUnbind(@Validated @RequestBody Map<String, Object> map) throws Exception {
        /** 小号解绑接口地址*/
        String url = "https://101.37.133.245:11008/voice/1.0.0/middleNumberUnbind";

        PrivacyUnbindBody unbindBody = new PrivacyUnbindBody();
        String str = "13041430152";

        String phone = "";//用户手机号
        String phone1 = "";//专家手机号

        /** 获取当前系统时间戳*/
        long timestamp = System.currentTimeMillis();
        /** 设置需要解绑的小号*/
        String trumpet =str;
        unbindBody.setMiddleNumber(trumpet);
        /** 设定与该隐私小号绑定的号码A*/
        unbindBody.setBindNumberA(phone1);
        /** 设定与该隐私小号绑定的号码B*/
        unbindBody.setBindNumberB(phone);

        String name = 494608+":"+timestamp;
        byte[] bytes = name.getBytes();
        /** 生成Base64转码后的参数authorization*/
        String authorization = Base64.encodeBase64String(bytes);

        /** 生成加密参数sig*/
        String sig = MD5Util.getMD5(494608 + "7d22b0d568ba4fe1ac4e842ec98de9cd" + timestamp);

        /** 生成最终接口访问地址*/
        url = url + "/" + 494608 + "/" + sig;

        String body = JSON.toJSONString(unbindBody);
        /** 调用接口解绑小号*/
        HttpsService httpsService = new HttpsService();
        String result = httpsService.doPost(url, authorization, body, "UTF-8");
        System.out.println("----------------------------------"+result);
        return R.ok(result);
    }

}
